package com.sdhs.system.syncdata.util;

import javax.crypto.*;
import javax.crypto.spec.*;
import java.util.*;

public class PassWordEncoder {

	public static String aesEncrypt(String str, String key) throws Exception {
		if (str == null || key == null)
			return null;
		Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
		cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key.getBytes("utf-8"), "AES"));
		byte[] bytes = cipher.doFinal(str.getBytes("utf-8"));
		return Base64.getEncoder().encodeToString(bytes);
	}

	public static String aesDecrypt(String str, String key) throws Exception {
		if (str == null || key == null)
			return null;
		Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
		cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key.getBytes("utf-8"), "AES"));
		byte[] bytes = Base64.getDecoder().decode(str);
		bytes = cipher.doFinal(bytes);
		return new String(bytes, "utf-8");
	}
}
